package member.base.action;

import m.common.action.ActionRequest;
import m.common.action.Param;
import m.common.action.RequestMethod;
import m.common.model.util.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import member.base.model.LevelUpSet;
import member.base.model.UserInfo;
import member.base.util.PowerUtil;

@ActionRequest(name = "action/base/levelUpSet",description = "级别升级")
public class LevelUpSetAction extends BaseAction{

    @RequestMethod(description = "列表")
    public JSONMessage getList() throws Exception {
        ModelQueryList queryList=toQueryList(LevelUpSet.class,"*","beforeLevel.*","afterLevel.*");
        queryList.addCondition(QueryCondition.eq("business.oid", PowerUtil.getBusiness().getOid()));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }
    @RequestMethod(description = "保存")
    public JSONMessage save(@Param("model") LevelUpSet model) throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerManage);
        model.setBusiness(PowerUtil.verifyBusiness(model.getBusiness()));
        ModelQueryCheck.init(model)
                .checkNotNull("beforeLevel.oid","当前会员不能为空")
                .checkNotNull("afterLevel.oid","升级后会员不能为空")
                .checkNotNull("useMoney","消费金额不能为空")
                .checkNotNull("orderNum","订单数不能为空")
                .doCheck(false,true);
        ModelCheckUtil.checkUniqueCombine(model,new String[]{"business.oid","beforeLevel.oid"},"当前会员升级配置已存在");
        if(model.getBeforeLevel().getOid().equals(model.getAfterLevel().getOid())){
            throw new MException("当前会员和升级后会员不能一样");
        }
        ModelUpdateUtil.saveModel(model);
        return JSONMessage.success("保存成功");
    }
}
